/**
 * 常见的工具类
 */
import { oldVisionCompany } from "@/common/index/config/constData"


/**
 * 获取屏幕大小
 */
export const getBodySize = () => {
    const w = uni.getWindowInfo().windowWidth
    const h = uni.getWindowInfo().windowHeight
    return { w, h }
}
//(min-width: 400px) and (min-height:550px) and (max-width: 700px) and (max-height:1500px)
/**
 * 判断是否为手机
 */
export const isPhone = () => {
    const { w, h } = getBodySize()
    if (w >= 330 && h >= 550 && w <= 700 && h <= 1500) {
        return true
    }
    return false
}
/**
 * 
 * @param {number} dag 角度 90°
 * @returns 
 */
export const dagToSinAndCos = (dag) => {
    const g = dag * Math.PI / 180
    return { sin: Math.sin(g), cos: Math.cos(g) }
}
/**
 * 将视力数据转换成索引
 * @param {string} vision 
 * @returns {number}
 */
export const parseVisionCompanyToindex = (vision) => {
    for (let i = 0; i < oldVisionCompany.length; i++) {
        if (vision == oldVisionCompany[i]) {
            return i
        }
    }
    return 0
}
/**
 * 将索引转换成视力数据
 * @param {number} index 
 * @returns {string}
 */
export const indexToVisionComany = (index) => {
    return index < 0 ? "0" : oldVisionCompany[index]
}

/**
 * 对象拷贝
 */
export const objectCopy = o => JSON.parse(JSON.stringify(o))
/**
 * 获取随机ID
 */
export const UUID = () => Math.floor(Math.random() * 100000) + ""



/**
 * 退出该页面
 */
export const exitPage = () => {
    uni.navigateBack({ delta: 1 })
}

/**
 * 
 * push视力测试 ,testTitle:测试主题，distance测试距离，initTestCompanyIndex：开始测试单位索引，lockDistance：是否锁定测量距离
 *
 */
export const pushTestVision = ({ testTitle, distance, initTestCompanyIndex, uuid, lockDistance }) => {
    if (!lockDistance) {
        lockDistance = false
    }
    let url = `/pages/testVision/testVision?testTitle=${testTitle}&distance=${distance}&initTestCompanyIndex=${initTestCompanyIndex}&uuid=${uuid}&lockDistance=${lockDistance}`
    uni.navigateTo({ url })
}
/**
 * push签名
 */
export const pushAutograph = ({ title, uuid, isRectangle, selectTrainer }) => {
    if (!selectTrainer) {
        selectTrainer = false
    }
    uni.navigateTo({
        url: `/pages/autograph/autograph?title=${title}&uuid=${uuid}&isRectangle=${isRectangle == true}&selectTrainer=${selectTrainer}`
    })
}
/**
 * 跳转登录
 */
export const toLogin = () => {
    uni.redirectTo({
        url: "/pages/index/index"
    })
}


/**
 * push散光测试
 */
export const pushTestAstigmatism = ({ uuid }) => {
    uni.navigateTo({
        url: `/pages/testAstigmatism/testAstigmatism?uuid=${uuid}`,
    })
}

/**
 * 转到main页面
 */
export const redirectMain = () => {
    uni.redirectTo({
        url: "/pages/main/main"
    })
}
/**
 * base64转png文件
 * @param {string} dataurl base64字符
 * @param {string} filename 文件名
 * @returns 
 */
export const base64ImgtoFile = (dataurl, filename = 'file') => {
    let arr = dataurl.split(',')
    console.log(arr)
    let mime = arr[0].split(':')[1].split(';')[0]
    let suffix = mime.split('/')[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], `${filename}.${suffix}`, {
        type: mime
    })
}
/**
 * 透明裁剪
 * @param {Uint8Array} data 
 * @param {number} w 
 * @param {number} h 
 * @returns 
 */
export function trimImage ({ data, width, height }) {
    try {
        const pixels = data
        let left = width
        let right = 0
        let top = height
        let bottom = 0
        for (let i = 0; i < pixels.length; i += 4) {
            const alpha = pixels[i + 3]
            if (alpha > 0) {
                const x = (i / 4) % width
                const y = Math.floor(i / (4 * width))
                left = Math.min(left, x)
                right = Math.max(right, x)
                top = Math.min(top, y)
                bottom = Math.max(bottom, y)
            }
        }
        if (right < left || bottom < top) {
            return null // no pixels were found
        }
        let trimmedWidth = right - left + 1
        let trimmedHeight = bottom - top + 1

        while (trimmedHeight > trimmedWidth) {
            if (left > 0 && trimmedWidth < width - 1) {
                left -= 1
                trimmedWidth += 2
            } else {
                break
            }
        }
        return { x: left, y: top, w: trimmedWidth, h: trimmedHeight }
    } catch {
        return null
    }
}
export const setDatabaseByKey = (key, data) => uni.setStorage({ key, data })
export const getDatabaseByKey = (key) => uni.getStorageSync(key)